package com.xy.webadmin.app.constant;

/**
 * MQTT模块常量定义
 *
 * @author xiaoyue
 * @date 2025-07-10
 */
public class MqttConstants {

    /**
     * MQTT协议版本
     */
    public static class ProtocolVersion {
        /** MQTT协议版本3.1 */
        public static final String V3_1 = "3.1";
        /** MQTT协议版本3.1.1 */
        public static final String V3_1_1 = "3.1.1";
        /** MQTT协议版本5.0 */
        public static final String V5_0 = "5.0";
    }

    /**
     * QoS级别
     */
    public static class QosLevel {
        /** QoS 0 - 最多一次 */
        public static final int AT_MOST_ONCE = 0;
        /** QoS 1 - 至少一次 */
        public static final int AT_LEAST_ONCE = 1;
        /** QoS 2 - 精确一次 */
        public static final int EXACTLY_ONCE = 2;
        /** 默认QoS级别 */
        public static final int DEFAULT = AT_LEAST_ONCE;
    }

    /**
     * 环境标识
     */
    public static class Environment {
        /** 开发环境 */
        public static final String DEV = "dev";
        /** 测试环境 */
        public static final String TEST = "test";
        /** 生产环境 */
        public static final String PROD = "prod";
    }

    /**
     * MQTT服务商类型
     */
    public static class BrokerType {
        /** EMQX服务商 */
        public static final String EMQX = "emqx";
    }

    /**
     * 缓存相关常量
     */
    public static class Cache {
        /** MQTT缓存名称前缀 */
        public static final String PREFIX = "mqtt:";
        /** 设备状态缓存名称 */
        public static final String DEVICE_STATUS = "mqttDeviceStatus";
        /** 按公司分组的设备状态缓存名称 */
        public static final String DEVICE_STATUS_BY_COMPANY = "mqttDeviceStatusByCompany";
        /** 默认缓存过期时间(秒) */
        public static final int DEFAULT_TTL = 300; // 5分钟
    }

    /**
     * MQTT连接配置默认值
     */
    public static class ConnectionDefaults {
        /** 默认心跳间隔(秒) */
        public static final int KEEP_ALIVE_INTERVAL = 60;
        /** 默认连接超时(秒) */
        public static final int CONNECTION_TIMEOUT = 30;
        /** 默认最大并发消息数 */
        public static final int MAX_INFLIGHT = 100;
        /** 默认重连最大延迟(毫秒) */
        public static final int MAX_RECONNECT_DELAY = 5000;
        /** 默认客户端ID前缀 */
        public static final String CLIENT_ID_PREFIX = "xy_mqtt_";
        /** 默认遗嘱消息主题 */
        public static final String DEFAULT_WILL_TOPIC = "$SYS/disconnect";
        /** 默认遗嘱消息内容 */
        public static final String DEFAULT_WILL_MESSAGE = "Client disconnected";
    }

    /**
     * API和端口相关常量
     */
    public static class ApiEndpoints {
        /**
         * EMQX API基础路径
         */
        public static final String EMQX_API_BASE = "/api/v5";

        /**
         * 节点信息端点
         */
        public static final String NODES_ENDPOINT = "/nodes";

        /**
         * 客户端信息端点
         */
        public static final String CLIENTS_ENDPOINT = "/clients";

        /**
         * 订阅信息端点
         */
        public static final String SUBSCRIPTIONS_ENDPOINT = "/subscriptions";

        /**
         * 统计信息端点
         */
        public static final String STATS_ENDPOINT = "/stats";

        /**
         * 默认HTTP超时（毫秒）
         */
        public static final int HTTP_TIMEOUT = 5000;

        /**
         * EMQX HTTP API默认端口
         */
        public static final int EMQX_HTTP_PORT = 18083;

        /**
         * EMQX HTTPS API默认端口
         */
        public static final int EMQX_HTTPS_PORT = 18084;
    }

    /**
     * HTTP相关常量
     */
    public static class Http {
        /**
         * HTTP协议前缀
         */
        public static final String HTTP_PREFIX = "http://";

        /**
         * HTTPS协议前缀
         */
        public static final String HTTPS_PREFIX = "https://";

        /**
         * MQTT协议前缀
         */
        public static final String MQTT_PREFIX = "mqtt://";

        /**
         * MQTTS协议前缀
         */
        public static final String MQTTS_PREFIX = "mqtts://";

        /**
         * 认证头名称
         */
        public static final String AUTH_HEADER = "Authorization";

        /**
         * Basic认证前缀
         */
        public static final String BASIC_AUTH_PREFIX = "Basic ";
    }

    /**
     * API响应字段常量
     */
    public static class ApiResponseFields {
        /**
         * 数据字段
         */
        public static final String DATA = "data";

        /**
         * 连接计数字段
         */
        public static final String CONNECTIONS_COUNT = "connections.count";

        /**
         * 会话计数字段 (代表总设备数)
         */
        public static final String SESSIONS_COUNT = "sessions.count";
    }

    /**
     * Spring Integration相关常量
     */
    public static class SpringIntegration {
        /** 默认MQTT输入通道名称 */
        public static final String MQTT_INPUT_CHANNEL = "mqttInputChannel";
        /** 默认MQTT输出通道名称 */
        public static final String MQTT_OUTPUT_CHANNEL = "mqttOutputChannel";
        /** 默认主题名称 */
        public static final String DEFAULT_TOPIC = "xy/default";
        /** MQTT消息Header - 主题 */
        public static final String HEADER_TOPIC = "mqtt_topic";
        /** MQTT消息Header - QoS */
        public static final String HEADER_QOS = "mqtt_qos";
        /** MQTT消息Header - 保留 */
        public static final String HEADER_RETAINED = "mqtt_retained";
        /** MQTT消息Header - 接收主题 */
        public static final String HEADER_RECEIVED_TOPIC = "mqtt_receivedTopic";
        /** Bean名称前缀 - 输出处理器 */
        public static final String BEAN_NAME_OUTBOUND_HANDLER = "mqttOutboundHandler_";
        /** Bean名称前缀 - 输入适配器 */
        public static final String BEAN_NAME_INBOUND_ADAPTER = "mqttInboundAdapter_";
        /** 默认Bean名称 - MQTT输出处理器 */
        public static final String DEFAULT_OUTBOUND_HANDLER = "defaultMqttOutboundHandler";
    }

    /**
     * 监控和健康检查相关常量
     */
    public static class Monitoring {
        /** 健康检查间隔(毫秒) - 5分钟 */
        public static final long HEALTH_CHECK_INTERVAL = 300000;
        /** 连接恢复重试次数 */
        public static final int RECONNECT_MAX_ATTEMPTS = 3;

        /** 设备状态汇总统计字段 */
        public static class StatusFields {
            /** 总设备数 */
            public static final String TOTAL_DEVICES = "totalDevices";
            /** 在线设备数 */
            public static final String ONLINE_DEVICES = "onlineDevices";
            /** 离线设备数 */
            public static final String OFFLINE_DEVICES = "offlineDevices";
            /** 配置计数 */
            public static final String CONFIG_COUNT = "configCount";
        }
    }

    /**
     * 定时任务相关常量
     */
    public static class Schedule {
        /** 缓存刷新间隔(毫秒) - 10分钟 */
        public static final long CACHE_REFRESH_INTERVAL = 300000;
        /** 健康检查间隔(毫秒) - 5分钟 */
        public static final long HEALTH_CHECK_INTERVAL = 300000;
        /** 主题订阅检查间隔(毫秒) - 30分钟 */
        public static final long SUBSCRIPTION_CHECK_INTERVAL = 1800000;
    }

    /**
     * URL相关常量
     */
    public static class Url {
        /**
         * TCP协议前缀
         */
        public static final String TCP_PREFIX = "tcp://";

        /**
         * SSL协议前缀
         */
        public static final String SSL_PREFIX = "ssl://";

        /**
         * 默认MQTT端口
         */
        public static final int DEFAULT_MQTT_PORT = 1883;

        /**
         * 默认MQTT SSL端口
         */
        public static final int DEFAULT_MQTT_SSL_PORT = 8883;
    }
}